package com.zxn.keyboard;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.inputmethodservice.Keyboard;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.zxn.keyboard.action.IKeyBoardUI;
import com.zxn.keyboard.action.KeyBoardActionListener;
import com.zxn.keyboard.impl.SystemOnKeyboardActionListener;
import com.zxn.keyboard.keyboard.CustomKeyboardView;
import com.zxn.keyboard.keyboard.MyKeyboardView;
import com.zxn.keyboard.util.Util;

/**
 * 仿微信输入红包页面的键盘.
 * 说明：使用系统API实现的键盘
 */
public class CustomKeyboardLayout extends FrameLayout {
    private static final String TAG = "CustomKeyboardLayout";
    private CustomKeyboardView keyboardView;
    private Drawable keybgDrawable;
    private Drawable keyDoneBgDrawable;
    private Keyboard mKeyboard;
    private SystemOnKeyboardActionListener actionListener;

    public CustomKeyboardLayout(Context context) {
        this(context, null);
    }

    public CustomKeyboardLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomKeyboardLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, @Nullable AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomKeyboardLayout);
        if (a.hasValue(R.styleable.CustomKeyboardLayout_keyViewBg)) {
            keybgDrawable = a.getDrawable(R.styleable.CustomKeyboardLayout_keyViewBg);
        }

        if (a.hasValue(R.styleable.CustomKeyboardLayout_keyDoneBg)) {
            keyDoneBgDrawable = a.getDrawable(R.styleable.CustomKeyboardLayout_keyDoneBg);
        }

        if (a.hasValue(R.styleable.CustomKeyboardLayout_xmlLayoutResId)) {
            int xmlLayoutResId = a.getResourceId(R.styleable.CustomKeyboardLayout_xmlLayoutResId, 0);
            initKeyBoard(context, xmlLayoutResId);
        }
        a.recycle();
    }

    private void initKeyBoard(Context context, int xmlLayoutResId) {
        mKeyboard = new Keyboard(context, xmlLayoutResId);
        keyboardView = (CustomKeyboardView) LayoutInflater.from(context).inflate(R.layout.custom_keyboard, null);
        keyboardView.setKeyboard(mKeyboard);
        keyboardView.setEnabled(true);
        keyboardView.setPreviewEnabled(false);
        if (null != keybgDrawable) {
            keyboardView.setKeyBgDrawable(keybgDrawable);
        }
        if (null != keyDoneBgDrawable) {
            keyboardView.setDoneBgDrawable(keyDoneBgDrawable);
        }
        actionListener = new SystemOnKeyboardActionListener();
        keyboardView.setOnKeyboardActionListener(actionListener);
        this.removeAllViews();
        this.addView(keyboardView);
    }


    public CustomKeyboardView getKeyboardView() {
        return keyboardView;
    }

    public Keyboard getKeyboard() {
        return mKeyboard;
    }


    /**
     * 设置键盘布局
     *
     * @param xmlLayoutResId xml
     *                       具体属性可参考：https://blog.csdn.net/ysmile1158157874/article/details/51497503
     */
    public void setXmlLayoutResId(int xmlLayoutResId) {
        initKeyBoard(getContext(), xmlLayoutResId);
    }


    /**
     * 设置按压背景，线条粗细等
     *
     * @param keybgDrawable d
     */
    public void setKeybgDrawable(Drawable keybgDrawable) {
        this.keybgDrawable = keybgDrawable;
        if (null != keyboardView)
            keyboardView.setKeyBgDrawable(keybgDrawable);
    }


    /**
     * 建立与EditText的绑定关系，用于控制输入值
     *
     * @param editText 绑定EditText 默认显示自定义键盘
     */
    public void setEditText(@NonNull EditText editText) {
        setEditText(editText, false);
    }


    /**
     * 建立与EditText的绑定关系，用于控制输入值
     *
     * @param editText             需要绑定的EditText
     * @param isOpenNativeKeyBoard 是否打开原生键盘
     */
    public void setEditText(@NonNull EditText editText, boolean isOpenNativeKeyBoard) {
        actionListener.setEditText(editText);
        if (isOpenNativeKeyBoard) {
            Util.showKeyboard(editText);
            setVisibility(GONE);
        } else {
            setVisibility(VISIBLE);
            Util.disableShowSoftInput(editText);
            Util.hideKeyboard(editText.getContext());
        }
    }


    /**
     * 设置键盘输入监听
     *
     * @param listener l
     */
    public void setOnKeyboardActionListener(KeyBoardActionListener listener) {
        actionListener.setKeyActionListener(listener);
    }

    /**
     * 设置ui监听
     *
     * @param ui u
     */
    public void setKeyboardUI(IKeyBoardUI ui) {
        if (null != ui) {
            keyboardView.setPaint(ui.setPaint(keyboardView.getPaint()));
        }
    }

}
